package Ch02.Shili.Sl1;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class Demo2 {
    public static void main(String[] args)throws Exception {
        //得到Dom解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析xml文档，得到一个Document对象，即Dom树
        Document doc = db.parse("src/Ch02/Shili/Sl1/book.xml");
        //读取pubDate
        NodeList list = doc.getElementsByTagName("pubDate");
        //pubDate元素节点
        Element pubDateElement=((Element) list.item(0));
        //读取文本节点
        String pudate=pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pudate);
    }
}
